package com.xianzhiOA.listener;

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.websocket.MsgSocket;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.websocket.Session;
import java.io.IOException;

/**
 * Created by Administrator on 2017/5/11.
 */
public class SessionCounter implements HttpSessionListener{
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("session创建");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("session消亡");
        HttpSession ctx = httpSessionEvent.getSession( );
        Session session = (Session) ctx.getAttribute("mySocket");
        XzEmp emp = (XzEmp) ctx.getAttribute("userForeEnd");
        System.out.println(session);
        if(emp!=null){
            MsgSocket.sessionMap.remove(emp.getEmpId());
        }
        if(session!=null){
            try {
                session.getBasicRemote().sendText("gunCu");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
